package com.leezk.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leezk.order.constant.CommonConstant;
import com.leezk.order.dto.OrderDto;
import com.leezk.order.dto.PayDto;
import com.leezk.order.dto.ResultDTO;
import com.leezk.order.entity.Orders;
import com.leezk.order.service.IOrdersService;
import com.leezk.order.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@RestController
@RequestMapping(CommonConstant.API_URI_VERSION_V1+"/orders")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;

    @GetMapping("/getOrders")
    public ResultDTO<Page<OrderDto>> getOrders(int pageNum, int pageSize){
        Page<Orders> page=new Page<>(pageNum,pageSize);
        return ResultDTO.success(ordersService.getOrders(page));
    }

    @GetMapping("/getAllOrders")
    public ResultDTO<List<OrderDto>> getAllOrders(){
        return ResultDTO.success(ordersService.getAllOrders());
    }

    @GetMapping("/getOrdersById")
    public ResultDTO<List<OrderDto>> getOrdersById(int sid){
        List<OrderDto> allOrders = ordersService.getAllOrders();
        System.out.println(allOrders);
        allOrders.removeIf(orderDto -> !Objects.equals(orderDto.getStudent().getId(),sid));
        return ResultDTO.success(allOrders);
    }

    @PostMapping("/createOrder")
    public ResultDTO<String> createOrder(@RequestBody OrderDto orders){
        return ResultDTO.success(ordersService.createOrder(orders));
    }

    @GetMapping("/payOrder")
    public ResultDTO<Boolean> payOrder(String orderId){
        return ResultDTO.success(ordersService.payOrder(orderId));
    }

}
